package kr.or.kead.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import kr.or.kead.domain.Auth;
import kr.or.kead.module.EmailCheck;
import kr.or.kead.service.DaoAuth;
import kr.or.kead.ui.insert_update.ProfessorUpdate;
import kr.or.kead.ui.insert_update.StdInsertUpdate;

public class Login extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel cp;
	private JPanel sp;
	private JLabel lblNewLabel;
	private EmailCheck txtEmail;
	private JLabel lblNewLabel_1;
	private JPasswordField txtpass;
	private JButton btnLogin;
	private DaoAuth daoAuth;
	private Auth auth; 
	private JPanel tp;
	private JLabel lblNewLabel_2;
	private JButton btnJoin;
	private JButton btnPassCheck;
	private JPanel lp;
	private JPanel txtp;
	public Login() {
		initialize();
		daoAuth = new DaoAuth();
		setVisible(true);
	}
	
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 448, 164);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		tp = new JPanel();
		tp.setBackground(Color.BLUE);
		contentPane.add(tp, BorderLayout.NORTH);
		tp.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		lblNewLabel_2 = new JLabel("대구직업능력개발원입니다");
		lblNewLabel_2.setBackground(Color.BLUE);
		lblNewLabel_2.setForeground(Color.ORANGE);
		tp.add(lblNewLabel_2);
		
		cp = new JPanel();
		contentPane.add(cp, BorderLayout.CENTER);
		cp.setLayout(new BorderLayout(0, 0));
		
		lp = new JPanel();
		cp.add(lp, BorderLayout.WEST);
		lp.setLayout(new GridLayout(2, 1, 5, 2));
		
		lblNewLabel = new JLabel("E-MAIL :");
		lp.add(lblNewLabel);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		lblNewLabel_1 = new JLabel("PASSWORD :");
		lp.add(lblNewLabel_1);
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		
		txtp = new JPanel();
		cp.add(txtp, BorderLayout.CENTER);
		txtp.setLayout(new GridLayout(2, 1, 5, 2));
		
		txtEmail = new EmailCheck();
		txtp.add(txtEmail);
		
		txtpass = new JPasswordField();
		txtp.add(txtpass);
		
		sp = new JPanel();
		contentPane.add(sp, BorderLayout.SOUTH);
		
		btnLogin = new JButton("로그인");
		btnLogin.setFont(new Font("HY산B", Font.PLAIN, 12));
		btnLogin.addActionListener(this);
		sp.add(btnLogin);
		
		btnJoin = new JButton("회원가입");
		btnJoin.addActionListener(this);
		btnJoin.setFont(new Font("HY산B", Font.PLAIN, 12));
		sp.add(btnJoin);
		
		btnPassCheck = new JButton("비밀번호찾기");
		btnPassCheck.addActionListener(this);
		btnPassCheck.setFont(new Font("HY산B", Font.PLAIN, 12));
		sp.add(btnPassCheck);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnJoin) {
			btnJoinActionPerformed(e);
		}
		if (e.getSource() == btnPassCheck) {
			btnPassCheckActionPerformed(e);
		}
		if (e.getSource() == btnLogin) {
			btnLoginActionPerformed(e);
		}
	}
	
	protected void btnLoginActionPerformed(ActionEvent e) {
		if(isPasswdCorrect(txtpass.getPassword())){
			StringTokenizer st = new StringTokenizer((String)daoAuth.selectTableByEmail(txtEmail.getEmail()),":");
			auth = new Auth(st.nextToken(), Integer.parseInt(st.nextToken()));
			dispose();
			new MainFrame(auth);
		}else{
			JOptionPane.showMessageDialog(null, "아이디 또는 패스워드 불일치");
			txtEmail.requestFocus();
			return;
		}
	}
	
	private boolean isPasswdCorrect(char[] input){
		boolean isCorrect =true;
		char[] correctPasswd = daoAuth.selectPasswdByEmail(txtEmail.getEmail().trim()).toCharArray();
		if (input.length != correctPasswd.length){
			isCorrect = false;
		}else{
			isCorrect = Arrays.equals(input, correctPasswd);
		}
		Arrays.fill(correctPasswd, '0');
		return isCorrect;
	}
	protected void btnPassCheckActionPerformed(ActionEvent e) {
		String email = JOptionPane.showInputDialog(null, "E-mail 입력").trim();
		
		String pass = daoAuth.selectPasswdByEmail(email);
		if (pass==null)JOptionPane.showMessageDialog(null, "해당 비밀번호가 존재하지 않습니다.");
		else {
			JOptionPane.showMessageDialog(null, "당신의 비밀번호는 "+pass+"입니다!");
			txtEmail.setEmail(email);;
			txtpass.setText(pass);
		}
	}
	protected void btnJoinActionPerformed(ActionEvent e) {
		new LevelCheck();
	}
}
